package com.shushan.util;

import android.app.ActivityManager;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.widget.LinearLayout;

import com.shushan.base.Constants;
import com.shushan.exam.bean.ExerciseBean;
import com.shushan.exam.bean.ExerciseOptionBean;
import com.shushan.exam.bean.PaperBean;
import com.shushan.home.bean.RewardTimeBean;
import com.shushan.home.bean.RewardToyBean;
import com.shushan.login.bean.ChildBean;
import com.shushan.login.bean.UserBean;
import com.shushan.mixedexam.bean.MutiExerciseBean;
import com.shushan.mixedexam.bean.MutiExerciseOptionBean;
import com.shushan.mixedexam.bean.MutiExercisePaperBean;
import com.shushan.mixedexam.englishwords.EnglishWordsExam3Fragment;
import com.shushan.mixedexam.englishwords.EnglishWordsExamFragment;
import com.shushan.mixedexam.hearing.HearingExamFragment;
import com.shushan.mixedexam.oralcalculation.bean.OralCalculateExerciseBean;
import com.shushan.mixedexam.speaking.SpeakingExamFragment;
import com.shushan.mixedexam.strangewords.StrangeWordsExam2Fragment;
import com.shushan.mixedexam.strangewords.StrangeWordsExamFragment;
import com.shushan.mixedexam.sync.ExamFragment;
import com.shushan.mixedexam.sync.FillBlankExamFragment;
import com.shushan.receiver.FreeLockReceiver;
import com.shushan.receiver.FreeWarnReceiver;

import org.json.JSONArray;
import org.json.JSONException;

import java.io.File;
import java.util.HashMap;
import java.util.List;

import static android.content.Context.ALARM_SERVICE;


/**
 * @ClassName: ToolUtils
 * @Desciption: 工具类
 * @author: yichaohua
 * @date: 2018-01-31
 */
public class ToolUtils {

    private static final String TAG = "ToolUtils";


    public static boolean containOptionId(String answerContent, int optionId) {
        if (TextUtils.isEmpty(answerContent) || optionId < 0) {
            return false;
        }

        JSONArray jsonArray = null;
        try {
            jsonArray = new JSONArray(answerContent);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        if (jsonArray == null) {
            return false;
        }

        for (int i = 0; i < jsonArray.length(); i++) {
            String id = jsonArray.optString(i);
            if (id.equals(optionId + "")) {
                return true;
            }
        }

        return false;
    }

    public static void setLockAlarm(Context context,int freeTime){
        SharedPreferencesUtils.setLongDate(Constants.START_GAME_TIME_SHAREPREFERS_KEY,System.currentTimeMillis());

        Log.i(TAG,"freeTime=" + freeTime);
        AlarmManager am = (AlarmManager)context.getSystemService(ALARM_SERVICE);

        if(freeTime > Constants.WARN_TIP_FORWARD_TIME){
            PendingIntent pendingIntent = PendingIntent.getBroadcast(context,0,
                    new Intent(context, FreeWarnReceiver.class),PendingIntent.FLAG_CANCEL_CURRENT);
            long atTimeInMillis = System.currentTimeMillis() + 1000*60*(freeTime - Constants.WARN_TIP_FORWARD_TIME);
            am.setExact(AlarmManager.RTC,atTimeInMillis,pendingIntent);
        }

        PendingIntent pendingIntent1 = PendingIntent.getBroadcast(context,0,
                new Intent(context, FreeLockReceiver.class),PendingIntent.FLAG_CANCEL_CURRENT);
        long atTimeInMillis1 = System.currentTimeMillis() + 1000*60*freeTime;
        am.setExact(AlarmManager.RTC_WAKEUP,atTimeInMillis1,pendingIntent1);
    }

    public static void cancelLockAlarm(Context context){
        Log.i(TAG,"cancelLockAlarm()");
        AlarmManager am = (AlarmManager)context.getSystemService(ALARM_SERVICE);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(context,0,
                new Intent(context, FreeWarnReceiver.class),PendingIntent.FLAG_CANCEL_CURRENT);
        am.cancel(pendingIntent);

        PendingIntent pendingIntent1 = PendingIntent.getBroadcast(context,0,
                new Intent(context, FreeLockReceiver.class),PendingIntent.FLAG_CANCEL_CURRENT);
        am.cancel(pendingIntent1);
    }

    public static String getExerciseTypeNameByTypeId(int typeId){
        switch (typeId){
            case Constants.TYPE_SYNC_EXERCISE:
                return "同步练习";
            case Constants.TYPE_SMART_EXERCISE:
                return "智能练习";
            case Constants.TYPE_LEARN_BY_DO:
                return "边学边练";
            case Constants.TYPE_KEY_POINT_EXERCISE:
                return "重点练习";
            case Constants.TYPE_DIFFICULT_POINT_EXERCISE:
                return "难点练习";
            case Constants.TYPE_EASY_WRONG_EXERCISE:
                return "易错练习";
            case Constants.TYPE_SPECIAL_TOPIC_EXERCISE:
                return "专项练习";
            case Constants.TYPE_STRANGE_WORDS_EXERCISE:
                return "生字练习";
            case Constants.TYPE_SPEAKING_EXERCISE:
                return "口语练习";
            case Constants.TYPE_ENGLISH_WORDS_EXERCISE:
                return "单词练习";
            case Constants.TYPE_HEARING_EXERCISE:
                return "听力练习";
            case Constants.TYPE_READ_RECITE_EXERCISE:
                return "朗读背诵";
            case Constants.TYPE_ORAL_CALCULATE_EXERCISE:
                return "口算练习";
            case Constants.TYPE_CHINESE_SECT_EXERCISE:
                return "分科练习";
            case Constants.TYPE_MACH_SECT_EXERCISE:
                return "分科练习";
            case Constants.TYPE_ENGLISH_SECT_EXERCISE:
                return "分科练习";
            case Constants.TYPE_V_CHINESE_EXERCISE:
                return "语文微课";
            case Constants.TYPE_V_MACH_EXERCISE:
                return "数学微课";
            case Constants.TYPE_V_ENGLISH_EXERCISE:
                return "英语微课";
            default:
                return "";
        }
    }

    public static String getTypeNameByTypeId(int typeId){
        switch (typeId){
            case Constants.TYPE_SYNC_EXERCISE:
                return "同步练习";
            case Constants.TYPE_SMART_EXERCISE:
                return "智能练习";
            case Constants.TYPE_LEARN_BY_DO:
                return "边学边练";
            case Constants.TYPE_KEY_POINT_EXERCISE:
                return "重点练习";
            case Constants.TYPE_DIFFICULT_POINT_EXERCISE:
                return "难点练习";
            case Constants.TYPE_EASY_WRONG_EXERCISE:
                return "易错练习";
            case Constants.TYPE_SPECIAL_TOPIC_EXERCISE:
                return "专项练习";
            case Constants.TYPE_STRANGE_WORDS_EXERCISE:
                return "生字练习";
            case Constants.TYPE_SPEAKING_EXERCISE:
                return "口语练习";
            case Constants.TYPE_ENGLISH_WORDS_EXERCISE:
                return "单词练习";
            case Constants.TYPE_HEARING_EXERCISE:
                return "听力练习";
            case Constants.TYPE_READ_RECITE_EXERCISE:
                return "朗读背诵";
            case Constants.TYPE_ORAL_CALCULATE_EXERCISE:
                return "口算练习";
            case Constants.TYPE_CHINESE_SECT_EXERCISE:
                return "语文分科练习";
            case Constants.TYPE_MACH_SECT_EXERCISE:
                return "数学分科练习";
            case Constants.TYPE_ENGLISH_SECT_EXERCISE:
                return "英语分科练习";
            case Constants.TYPE_V_CHINESE_EXERCISE:
                return "语文微课";
            case Constants.TYPE_V_MACH_EXERCISE:
                return "数学微课";
            case Constants.TYPE_V_ENGLISH_EXERCISE:
                return "英语微课";
            default:
                return "";
        }
    }

    public static String getSubjectNameFromId(int subjectId){
        String name = "";
        switch (subjectId){
            case Constants.SUBJECT_CHINESE_ID:
                name = "语文";
                break;
            case Constants.SUBJECT_MACH_ID:
                name = "数学";
                break;
            case Constants.SUBJECT_ENGLISH_ID:
                name = "英语";
                break;
            default:
                break;
        }

        return name;
    }

    public static boolean isListEmpty(List list) {
        if (list != null && list.size() > 0) {
            return false;
        }
        return true;
    }

    public static boolean hasSmartExercise(List<RewardToyBean> rewardToyBeans,List<RewardTimeBean> rewardTimeBeans){
        if(!isListEmpty(rewardToyBeans)){
            for (RewardToyBean rewardToyBean : rewardToyBeans){
                if(rewardToyBean.znlx){
                    return true;
                }
            }
        }

        if(!isListEmpty(rewardTimeBeans)){
            for (RewardTimeBean rewardTimeBean : rewardTimeBeans){
                if(rewardTimeBean.znlx){
                    return true;
                }
            }
        }

        return false;
    }

    public static void clearRecord(MutiExercisePaperBean examPaperBean) {
        if(examPaperBean == null){
            return;
        }

        List<MutiExerciseBean> learnByDoExerciseList = examPaperBean.getPaperBeans();
        if(!ToolUtils.isListEmpty(learnByDoExerciseList)){
            for (MutiExerciseBean exerciseBean : learnByDoExerciseList){
                List<MutiExerciseOptionBean> optionList = exerciseBean.getOptionBeans();
                for (MutiExerciseOptionBean optionBean : optionList){
                    optionBean.setAnswerIndex(-1);
                }
            }
        }

        List<MutiExerciseBean> chineseExerciseList = examPaperBean.getChineseExerciseBeans();
        if(!ToolUtils.isListEmpty(chineseExerciseList)){
            for (MutiExerciseBean exerciseBean : chineseExerciseList){
                List<MutiExerciseOptionBean> optionList = exerciseBean.getOptionBeans();
                for (MutiExerciseOptionBean optionBean : optionList){
                    optionBean.setAnswerIndex(-1);
                }
            }
        }

        List<MutiExerciseBean> mathExerciseList = examPaperBean.getMathExerciseBeans();
        if(!ToolUtils.isListEmpty(mathExerciseList)){
            for (MutiExerciseBean exerciseBean : mathExerciseList){
                List<MutiExerciseOptionBean> optionList = exerciseBean.getOptionBeans();
                for (MutiExerciseOptionBean optionBean : optionList){
                    optionBean.setAnswerIndex(-1);
                }
            }
        }

        List<MutiExerciseBean> englishExerciseList = examPaperBean.getEnglishExerciseBeans();
        if(!ToolUtils.isListEmpty(englishExerciseList)){
            for (MutiExerciseBean exerciseBean : englishExerciseList){
                List<MutiExerciseOptionBean> optionList = exerciseBean.getOptionBeans();
                for (MutiExerciseOptionBean optionBean : optionList){
                    optionBean.setAnswerIndex(-1);
                }
            }
        }

    }

    public static PaperBean getPaperBean(int subjectId, List<ExerciseBean> exerciseBeanList) {
        if(isListEmpty(exerciseBeanList)){
            return null;
        }

        PaperBean paperBean = new PaperBean();

        switch (subjectId){
            case Constants.SUBJECT_CHINESE_ID:
                paperBean.setChineseExerciseList(exerciseBeanList);
                break;
            case Constants.SUBJECT_MACH_ID:
                paperBean.setMathExerciseList(exerciseBeanList);
                break;
            case Constants.SUBJECT_ENGLISH_ID:
                paperBean.setEnglishExerciseList(exerciseBeanList);
                break;
            default:
                break;
        }

        return paperBean;
    }

    public static int[] getPaperNumForIndex(PaperBean paperBean){
        int numArray[] = {0,0};
        if(paperBean == null){
            return numArray;
        }

        List<ExerciseBean> chineseExerciseList = paperBean.getChineseExerciseList();
        if(!isListEmpty(chineseExerciseList)){
            for (ExerciseBean exerciseBean : chineseExerciseList){
                List<ExerciseOptionBean> optionList = exerciseBean.getOptionList();
                for (int i = 0;i < optionList.size();i++){
                    ExerciseOptionBean optionBean = optionList.get(i);
                    int answerIndex = optionBean.getAnswerIndex();
                    if( answerIndex >= 0){
                        ExerciseOptionBean resultOption = optionList.get(answerIndex);
                        if(resultOption.option_result == 1){
                            numArray[0]++;
                        }else {
                            numArray[1]++;
                        }
                        break;
                    }else if(i == (optionList.size()-1)){
                        numArray[1]++;
                    }
                }

            }
        }

        List<ExerciseBean> mathExerciseList = paperBean.getMathExerciseList();
        if(!isListEmpty(mathExerciseList)){
            for (ExerciseBean exerciseBean : mathExerciseList){
                List<ExerciseOptionBean> optionList = exerciseBean.getOptionList();
                for (int i = 0;i < optionList.size();i++){
                    ExerciseOptionBean optionBean = optionList.get(i);
                    int answerIndex = optionBean.getAnswerIndex();
                    if( answerIndex >= 0){
                        ExerciseOptionBean resultOption = optionList.get(answerIndex);
                        if(resultOption.option_result == 1){
                            numArray[0]++;
                        }else {
                            numArray[1]++;
                        }
                        break;
                    }else if(i == (optionList.size()-1)){
                        numArray[1]++;
                    }
                }

            }
        }

        List<ExerciseBean> englishExerciseList = paperBean.getEnglishExerciseList();
        if(!isListEmpty(englishExerciseList)){
            for (ExerciseBean exerciseBean : englishExerciseList){
                List<ExerciseOptionBean> optionList = exerciseBean.getOptionList();
                for (int i = 0;i < optionList.size();i++){
                    ExerciseOptionBean optionBean = optionList.get(i);
                    int answerIndex = optionBean.getAnswerIndex();
                    if( answerIndex >= 0){
                        ExerciseOptionBean resultOption = optionList.get(answerIndex);
                        if(resultOption.option_result == 1){
                            numArray[0]++;
                        }else {
                            numArray[1]++;
                        }
                        break;
                    }else if(i == (optionList.size()-1)){
                        numArray[1]++;
                    }
                }

            }
        }

        return numArray;
    }

    public static int[] getPaperNumForId(PaperBean paperBean){
        int numArray[] = {0,0};
        if(paperBean == null){
            return numArray;
        }

        List<ExerciseBean> chineseExerciseList = paperBean.getChineseExerciseList();
        if(!isListEmpty(chineseExerciseList)){
            for (ExerciseBean exerciseBean : chineseExerciseList){
                int myAnswerId = exerciseBean.my_answer_id;
                List<ExerciseOptionBean> optionList = exerciseBean.getOptionList();
                for (int i = 0;i < optionList.size();i++){
                    ExerciseOptionBean optionBean = optionList.get(i);
                    if(myAnswerId == optionBean.id){
                        if(optionBean.option_result == 1){
                            numArray[0]++;
                        }else {
                            numArray[1]++;
                        }
                        break;
                    }else if(i == (optionList.size()-1)){
                        numArray[1]++;
                    }
                }

            }
        }

        List<ExerciseBean> mathExerciseList = paperBean.getMathExerciseList();
        if(!isListEmpty(mathExerciseList)){
            for (ExerciseBean exerciseBean : mathExerciseList){
                int myAnswerId = exerciseBean.my_answer_id;
                List<ExerciseOptionBean> optionList = exerciseBean.getOptionList();
                for (int i = 0;i < optionList.size();i++){
                    ExerciseOptionBean optionBean = optionList.get(i);
                    if(myAnswerId == optionBean.id){
                        if(optionBean.option_result == 1){
                            numArray[0]++;
                        }else {
                            numArray[1]++;
                        }
                        break;
                    }else if(i == (optionList.size()-1)){
                        numArray[1]++;
                    }
                }

            }
        }

        List<ExerciseBean> englishExerciseList = paperBean.getEnglishExerciseList();
        if(!isListEmpty(englishExerciseList)){
            for (ExerciseBean exerciseBean : englishExerciseList){
                int myAnswerId = exerciseBean.my_answer_id;
                List<ExerciseOptionBean> optionList = exerciseBean.getOptionList();
                for (int i = 0;i < optionList.size();i++){
                    ExerciseOptionBean optionBean = optionList.get(i);
                    if(myAnswerId == optionBean.id){
                        if(optionBean.option_result == 1){
                            numArray[0]++;
                        }else {
                            numArray[1]++;
                        }
                        break;
                    }else if(i == (optionList.size()-1)){
                        numArray[1]++;
                    }
                }

            }
        }

        return numArray;
    }

    public static String getChildGradeStr(ChildBean childBean){
        StringBuffer sb = new StringBuffer();
        sb.append(childBean.gradeName)
                .append("   ")
                .append(childBean.semester.equals("UP")?"上学期":"下学期");
        return sb.toString();
    }

    public static boolean isNoData(List<HashMap<Integer,Integer>> list){
        int total = 0;
        for(int i=0;i<list.size();i++){
            HashMap<Integer,Integer> map = list.get(i);
            for(int key : map.keySet()){
                total+=map.get(key);
            }
        }
        if(total == 0){
            return true;
        }
        return false;
    }

    public static boolean isDefaultLauncher(Context context) {
        return context.getPackageName().equals(getDefaultLauncher(context));
    }

    public static String getDefaultLauncher(Context context) {
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        ResolveInfo resolveInfo = context.getPackageManager()
                .resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
        String currentHomePackage = resolveInfo.activityInfo.packageName;

        return currentHomePackage;
    }

    public static boolean isExamWrong(List<ExerciseOptionBean> optionList) {
        if(optionList == null || optionList.size() == 0){
            return false;
        }

        int answerIndex = -1;
        for (ExerciseOptionBean optionBean : optionList){
            if(optionBean.getAnswerIndex() >= 0){
                answerIndex = optionBean.getAnswerIndex();
                break;
            }
        }

        if(answerIndex < 0){
            return false;
        }

        return optionList.get(answerIndex).option_result==0?true:false;

    }

    public static boolean isExamWrongForSelectTopic(List<MutiExerciseOptionBean> optionList) {
        if(optionList == null || optionList.size() == 0){
            return false;
        }

        int answerIndex = -1;
        for (MutiExerciseOptionBean optionBean : optionList){
            if(optionBean.getAnswerIndex() >= 0){
                answerIndex = optionBean.getAnswerIndex();
                break;
            }
        }

        if(answerIndex < 0){
            return false;
        }

        return optionList.get(answerIndex).option_result==0?true:false;

    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    public static int getVipTypeForGoodsDetails(UserBean userBean){
        if(userBean.svip == 1){
            return 3;
        }else if(userBean.vip == 1){
            return 2;
        }else if(userBean.share_vip == 1){
            return 1;
        }else{
            return 0;
        }
    }

    public static boolean isWrongTopic(OralCalculateExerciseBean exerciseBean,boolean fromRecord){
        if(!fromRecord) {
            if(TextUtils.isEmpty(exerciseBean.my_answer)){
                return false;
            }
            if (!exerciseBean.my_answer.equals(exerciseBean.answer)) {
                return true;
            }
            return false;
        }else{
            if(TextUtils.isEmpty(exerciseBean.my_answer_id)){
                return false;
            }
            if (!exerciseBean.my_answer_id.equals(exerciseBean.answer)) {
                return true;
            }
            return false;
        }
    }

    public static boolean isWrongTopic(int exerciseLevel,MutiExerciseBean exerciseBean){
        boolean isWrongTopic = false;
        switch (Integer.parseInt(exerciseBean.exercises_type)){
            case Constants.TYPE_SYNC_EXERCISE://同步练习
                switch (exerciseLevel){
                    case 0://单选题
                        if(isExamWrongForSelectTopic(exerciseBean.getOptionBeans())){
                            isWrongTopic = true;
                        }
                        break;
                    case 1://填空题
                        if(!exerciseBean.my_option.equals(exerciseBean.answer)){
                            isWrongTopic = true;
                        }
                        break;
                }
                break;
            case Constants.TYPE_STRANGE_WORDS_EXERCISE://生字练习
            case Constants.TYPE_HEARING_EXERCISE://听力练习
                if(isExamWrongForSelectTopic(exerciseBean.getOptionBeans())){
                    isWrongTopic = true;
                }
                break;
            case Constants.TYPE_SPEAKING_EXERCISE://口语练习
                int score = getSpeakingScore(exerciseBean);
                if(score < 70){
                    isWrongTopic = true;
                }
                break;
            case Constants.TYPE_ENGLISH_WORDS_EXERCISE://单词练习
                switch (exerciseLevel){
                    case 0:
                    case 1:
                    case 3:
                        if(isExamWrongForSelectTopic(exerciseBean.getOptionBeans())){
                            isWrongTopic = true;
                        }
                        break;
                    case 4:
                        if(!exerciseBean.my_option.equals(exerciseBean.answer)){
                            isWrongTopic = true;
                        }
                        break;
                }
                break;
        }

        return isWrongTopic;
    }

    public static int getSpeakingScore(MutiExerciseBean exerciseBean){
        int score = 0;
        if(TextUtils.isEmpty(exerciseBean.my_option)){
            if(TextUtils.isEmpty(exerciseBean.my_answer_id_str) || exerciseBean.score == 0){
                return 0;
            }

            score = (int)((float)(100*Integer.parseInt(exerciseBean.my_answer_id_str))/(float)exerciseBean.score);
            Log.d(TAG,"getSpeakingScore1 : score == "+score+" ; "+exerciseBean.my_answer_id_str+" ; "+exerciseBean.score);
        }else{
            if(TextUtils.isEmpty(exerciseBean.my_option) || exerciseBean.score == 0){
                return 0;
            }

            score = (int)((float)(100*Integer.parseInt(exerciseBean.my_option))/(float)exerciseBean.score);
            Log.d(TAG,"getSpeakingScore2 : score == "+score+" ; "+exerciseBean.my_option+" ; "+exerciseBean.score);
        }
        return score;
    }

    public static MutiExerciseBean getMutiExerciseBean(Fragment fragment){
        MutiExerciseBean exerciseBean = null;

        if(fragment instanceof ExamFragment){//同步
            exerciseBean = ((ExamFragment)fragment).getExerciseBean();
        }else if(fragment instanceof FillBlankExamFragment){//同步
            exerciseBean = ((FillBlankExamFragment)fragment).getExerciseBean();
        }else if(fragment instanceof StrangeWordsExamFragment){//生字
            exerciseBean = ((StrangeWordsExamFragment)fragment).getExerciseBean();
        }else if(fragment instanceof StrangeWordsExam2Fragment){//生字
            exerciseBean = ((StrangeWordsExam2Fragment)fragment).getExerciseBean();
        }else if(fragment instanceof SpeakingExamFragment){//口语
            exerciseBean = ((SpeakingExamFragment)fragment).getExerciseBean();
        }else if(fragment instanceof EnglishWordsExamFragment){//单词
            exerciseBean = ((EnglishWordsExamFragment)fragment).getExerciseBean();
        }else if(fragment instanceof EnglishWordsExam3Fragment){//单词
            exerciseBean = ((EnglishWordsExam3Fragment)fragment).getExerciseBean();
        }else if(fragment instanceof HearingExamFragment){//听力
            exerciseBean = ((HearingExamFragment)fragment).getExerciseBean();
        }

        return exerciseBean;
    }

    public static int[] getExamFragmentType(Fragment fragment){
        int[] types = null;
        int type = 0;
        int subType = 0;

        if(fragment instanceof ExamFragment){//同步
//            examFragment = new ExamFragment();
            type = Constants.TYPE_SYNC_EXERCISE;
            subType = 0;
            types = new int[]{type,subType};
        }else if(fragment instanceof FillBlankExamFragment){//同步
//            examFragment = ((FillBlankExamFragment)fragment).getExerciseBean();
            type = Constants.TYPE_SYNC_EXERCISE;
            subType = 1;
            types = new int[]{type,subType};
        }else if(fragment instanceof StrangeWordsExamFragment){//生字
//            examFragment = ((StrangeWordsExamFragment)fragment).getExerciseBean();
            type = Constants.TYPE_STRANGE_WORDS_EXERCISE;
            subType = 0;
            types = new int[]{type,subType};
        }else if(fragment instanceof StrangeWordsExam2Fragment){//生字
//            examFragment = ((StrangeWordsExam2Fragment)fragment).getExerciseBean();
            type = Constants.TYPE_STRANGE_WORDS_EXERCISE;
            subType = 1;
            types = new int[]{type,subType};
        }else if(fragment instanceof SpeakingExamFragment){//口语
//            examFragment = ((SpeakingExamFragment)fragment).getExerciseBean();
            type = Constants.TYPE_SPEAKING_EXERCISE;
            subType = 0;
            types = new int[]{type,subType};
        }else if(fragment instanceof EnglishWordsExamFragment){//单词
//            examFragment = ((EnglishWordsExamFragment)fragment).getExerciseBean();
            type = Constants.TYPE_ENGLISH_WORDS_EXERCISE;
            subType = 0;
            types = new int[]{type,subType};
        }else if(fragment instanceof EnglishWordsExam3Fragment){//单词
//            examFragment = ((EnglishWordsExam3Fragment)fragment).getExerciseBean();
            type = Constants.TYPE_ENGLISH_WORDS_EXERCISE;
            subType = 1;
            types = new int[]{type,subType};
        }else if(fragment instanceof HearingExamFragment){//听力
//            examFragment = ((HearingExamFragment)fragment).getExerciseBean();
            type = Constants.TYPE_HEARING_EXERCISE;
            subType = 0;
            types = new int[]{type,subType};
        }

        return types;
    }

    public static String getLevelTermText(ChildBean childBean){
        if(childBean == null){
            return "";
        }
        String text = "";
        text = childBean.gradeName+" "+(childBean.semester.equals("UP")?"上":"下");
        return text;
    }

    public static String getPreThreeStr(String name){
        if(name.length() <= 3){
            return name;
        }else {
            StringBuffer sb = new StringBuffer();
            sb.append(name.substring(0, 3));
            sb.append("...");
            return sb.toString();
        }
    }

    public static void deleteFile(String path) {
        if (TextUtils.isEmpty(path)){
            return;
        }

        File file = new File(path);
        deleteFile(file);
    }

    public static void deleteFile(File file) {
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            for (int i = 0; i < files.length; i++) {
                File f = files[i];
                deleteFile(f);
            }
//            file.delete();//如要保留文件夹，只删除文件，请注释这行
        } else if (file.exists()) {
            file.delete();
        }
    }
}

